package com.free.logic.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.free.commons.form.DynamicForm;
import com.free.constants.FrameworkConstant;
import com.free.entity.UserEntity;
import com.free.logic.service.BaseService;
import com.free.tools.CollectionsUtils;

@Service("userService")
@Scope("prototype")
public class UserServiceImpl extends BaseService<UserEntity> implements IUserService {
	
	@Override
	public boolean saveUser(DynamicForm dynaForm) {
		UserEntity user = new UserEntity();
		CollectionsUtils.copyProperties(dynaForm, user);
		return dao.save(user) != null;
	}
	
	@Override
	public Map<String,Object> findUser(DynamicForm dynaForm)
	{
		String hql = "from User";
		List<UserEntity> userList = dao.query(hql, dynaForm.getPage(), dynaForm.getLimit());
		List<DynamicForm> dataList  = CollectionsUtils.copyToDynamicForm(userList);
		int count = dao.queryResultCount(hql);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put(FrameworkConstant.RESULT_COUNT, count);
		result.put(FrameworkConstant.RESULT_DATA, dataList);
		return result;
	}
	
	@Override
	public boolean removeUser(DynamicForm dynaForm)
	{
		dao.deleteByPk(UserEntity.class, dynaForm.get("id"));
		return true;
	}
	
	@Override
	public boolean editUser(DynamicForm dynaForm) throws Exception
	{
		UserEntity entity = new UserEntity();
		//dynaForm.set("id", dynaForm.get("id"));
		CollectionsUtils.copyProperties(dynaForm, entity);
		dao.saveOrUpdate(entity);
		return true;
	}

}
